package com.xyht.sca_s.student_manage_system.modules.payment.wxpay.config;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RSANotificationConfig;
import com.wechat.pay.java.service.payments.app.AppServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.profitsharing.ProfitsharingService;
import com.wechat.pay.java.service.refund.RefundService;
import com.xyht.sca_s.student_manage_system.modules.payment.wxpay.entity.WxPayCommonConfig;
import com.xyht.sca_s.student_manage_system.modules.payment.wxpay.entity.WxPayConfigProperties;
import com.xyht.sca_s.student_manage_system.modules.payment.wxpay.entity.WxPayFunctions;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Component
public class WxPayConfig {
    @Resource
    private WxPayConfigProperties wxPayConfigProperties;
    public static List<WxPayFunctions> wxPayFunctionsList = new ArrayList<>();

    /**
     * @description 微信支付初始化配置
     * 平台证书获取步骤：1、下载jar包  https://github.com/wechatpay-apiv3/CertificateDownloader
     * 2、运行jar包 java -jar CertificateDownloader.jar -k ${apiV3key} -m ${mchId} -f ${apiclient_key.pem路径} -s ${证书序列号} -o ${outputFilePath}
     *
     * @create 2023/12/24
     * @author dxn
    */
//    @Bean
    public void wxPayInit(){
        if (wxPayConfigProperties.getConfigs() == null || wxPayConfigProperties.getConfigs().size() < 1){
            return;
        }

        for (WxPayCommonConfig properties : wxPayConfigProperties.getConfigs()){
            String OSName = System.getProperty("os.name");
            String certificatePath = OSName.toLowerCase().startsWith("win") ? properties.WX_PLATE_CERT_PATH_WIN
                    : properties.WX_PLATE_CERT_PATH_LINUX;
            String keyPath = OSName.toLowerCase().startsWith("win") ? properties.WX_KEY_PATH_WIN
                    : properties.WX_KEY_PATH_LINUX;

            Config rsaConfig =
                    new RSAAutoCertificateConfig.Builder()
                            .merchantId(properties.WX_MERCHANT_ID)
                            .privateKeyFromPath(keyPath)
                            .merchantSerialNumber(properties.WX_MERCHANT_SERIALNUMBER)
                            .apiV3Key(properties.WX_API_V3_KEY)
                            .build();

            WxPayFunctions wxPayFunctions = new WxPayFunctions();
            wxPayFunctions.setJsapiServiceExtension(new JsapiServiceExtension.Builder().config(rsaConfig).build());
            wxPayFunctions.setRefundService(new RefundService.Builder().config(rsaConfig).build());
            wxPayFunctions.setProfitsharingService(new ProfitsharingService.Builder().config(rsaConfig).build());
            wxPayFunctions.setAppServiceExtension(new AppServiceExtension.Builder().config(rsaConfig).build());

            RSANotificationConfig notificationConfig = new RSANotificationConfig.Builder()
                    .certificatesFromPath(certificatePath)
                    .apiV3Key(properties.WX_API_V3_KEY)
                    .build();
            wxPayFunctions.setNotificationParser(new NotificationParser(notificationConfig));
            wxPayFunctions.setWxPayCommonConfig(properties);
            wxPayFunctionsList.add(wxPayFunctions);
        }
    }


}
